package com.novax.ex.activity.provider.handler.consumer;

import com.alibaba.fastjson.JSON;
import com.novax.ex.activity.infrastructure.entity.MallPrizeLottery;
import com.novax.ex.activity.infrastructure.entity.MallProduct;
import com.novax.ex.activity.provider.handler.provider.MallOrderProvider;
import com.novax.ex.activity.provider.service.MallPrizeService;
import com.novax.ex.common.constant.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.Objects;

/**
 * @Description 商品消息消费者
 * @Author zhaolei
 * @Date 2/21/23 4:22 PM
 * @Version 1.0
 */
@Slf4j
@Component
public class MallProductConsumer {
    @Resource
    private MallPrizeService mallPrizeService;
    @Resource
    private MallOrderProvider mallOrderProvider;
    /**
     * @description: 商品上架
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallProduct]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallProduct.Put_On_Queue, durable = "true"))
    public void putOn(String mallProduct) {
        try {
            log.info("商品消息跟踪putOn："+mallProduct);
            MallProduct product = JSON.parseObject(mallProduct, MallProduct.class);
            mallPrizeService.add(product);
        } catch (Exception e) {
            log.error("[商品上架消息消费异常] msg = {}", mallProduct, e);
        }
    }

    /**
     * @description: 商品删除
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallProduct]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallProduct.Del_Queue, durable = "true"))
    public void del(String mallProduct) {
        try {
            log.info("商品消息跟踪del："+mallProduct);
            MallProduct product = JSON.parseObject(mallProduct, MallProduct.class);
            mallPrizeService.refund(product);
        } catch (Exception e) {
            log.error("[商品删除消息消费异常] msg = {}", mallProduct, e);
        }
    }

    /**
     * @description: 商品售价变动
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallProduct]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallProduct.Update_Price_Queue, durable = "true"))
    public void updatePrice(String mallProduct) {
        try {
            log.info("商品售价变动消息跟踪updatePrice："+mallProduct);
            MallProduct product = JSON.parseObject(mallProduct, MallProduct.class);
            MallPrizeLottery mallPrizeLottery = mallPrizeService.updatePrice(product);
            if(Objects.nonNull(mallPrizeLottery)) {
                mallOrderProvider.sendPrize(JSON.toJSONString(mallPrizeLottery));
            }
        } catch (Exception e) {
            log.error("[商品售价变动消息消费异常] msg = {}", mallProduct, e);
        }
    }

    /**
     * @description: 商品成本价变动
     * @author: Wade
     * @date: 2/21/23 4:31 PM
     * @param: [mallProduct]
     * @return: void
     **/
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.MallProduct.Update_Cost_Queue, durable = "true"))
    public void updateCost(String mallProduct) {
        try {
            log.info("商品成本价变动消息跟踪updateCost："+mallProduct);
            MallProduct product = JSON.parseObject(mallProduct, MallProduct.class);
            mallPrizeService.updateCost(product);
        } catch (Exception e) {
            log.error("[商品成本价变动消息消费异常] msg = {}", mallProduct, e);
        }
    }
}
